1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.net.spi;
27
28 import java.net.InetSocketAddress;
29 import java.net.Proxy;
30 import java.net.ProxySelector;
31 import java.net.SocketAddress;
32 import java.net.URI;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.StringTokenizer;
36 import java.io.IOException;
37 import sun.misc.RegexpPool;
38 import java.security.AccessController;
39 import java.security.PrivilegedAction;
40 import sun.net.NetProperties;
41 import sun.net.SocksProxy;
42
43
44
45
46
47
48
49
50
51
52
53
54 public class DefaultProxySelector extends ProxySelector {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 final static String[][] props = {
75
76
77
78 {"http", "http.proxy", "proxy", "socksProxy"},
79 {"https", "https.proxy", "proxy", "socksProxy"},
80 {"ftp", "ftp.proxy", "ftpProxy", "proxy", "socksProxy"},
81 {"gopher", "gopherProxy", "socksProxy"},
82 {"socket", "socksProxy"}
83 };
84
85 private static final String SOCKS_PROXY_VERSION = "socksProxyVersion";
86
87 private static boolean hasSystemProxies = false;
88
89 static {
90 final String key = "java.net.useSystemProxies";
91 Boolean b = AccessController.doPrivileged(
92 new PrivilegedAction<Boolean>() {
93 public Boolean run() {
94 return NetProperties.getBoolean(key);
95 }});
96 if (b != null && b.booleanValue()) {
97 java.security.AccessController.doPrivileged(
98 new sun.security.action.LoadLibraryAction("net"));
99 hasSystemProxies = init();
100 }
101 }
102
103
104
105
106
107
108
109
110
111 static class NonProxyInfo {
112
113
114 static final String defStringVal = "localhost|127.*|[::1]";
115
116 String hostsSource;
117 RegexpPool hostsPool;
118 final String property;
119 final String defaultVal;
120 static NonProxyInfo ftpNonProxyInfo = new NonProxyInfo("ftp.nonProxyHosts", null, null, defStringVal);
121 static NonProxyInfo httpNonProxyInfo = new NonProxyInfo("http.nonProxyHosts", null, null, defStringVal);
122
123 NonProxyInfo(String p, String s, RegexpPool pool, String d) {
124 property = p;
125 hostsSource = s;
126 hostsPool = pool;
127 defaultVal = d;
128 }
129 }
130
131
132
133
134
135
136
137
138
139 public java.util.List<Proxy> select(URI uri) {
140 if (uri == null) {
141 throw new IllegalArgumentException("URI can't be null.");
142 }
143 String protocol = uri.getScheme();
144 String host = uri.getHost();
145
146 if (host == null) {
147
148
149
150
151
152
153
154 String auth = uri.getAuthority();
155 if (auth != null) {
156 int i;
157 i = auth.indexOf('@');
158 if (i >= 0) {
159 auth = auth.substring(i+1);
160 }
161 i = auth.lastIndexOf(':');
162 if (i >= 0) {
163 auth = auth.substring(0,i);
164 }
165 host = auth;
166 }
167 }
168
169 if (protocol == null || host == null) {
170 throw new IllegalArgumentException("protocol = "+protocol+" host = "+host);
171 }
172 List<Proxy> proxyl = new ArrayList<Proxy>(1);
173
174 NonProxyInfo pinfo = null;
175
176 if ("http".equalsIgnoreCase(protocol)) {
177 pinfo = NonProxyInfo.httpNonProxyInfo;
178 } else if ("https".equalsIgnoreCase(protocol)) {
179
180
181 pinfo = NonProxyInfo.httpNonProxyInfo;
182 } else if ("ftp".equalsIgnoreCase(protocol)) {
183 pinfo = NonProxyInfo.ftpNonProxyInfo;
184 }
185
186
187
188
189 final String proto = protocol;
190 final NonProxyInfo nprop = pinfo;
191 final String urlhost = host.toLowerCase();
192
193
194
195
196
197
198
199 Proxy p = AccessController.doPrivileged(
200 new PrivilegedAction<Proxy>() {
201 public Proxy run() {
202 int i, j;
203 String phost = null;
204 int pport = 0;
205 String nphosts = null;
206 InetSocketAddress saddr = null;
207
208
209 for (i=0; i<props.length; i++) {
210 if (props[i][0].equalsIgnoreCase(proto)) {
211 for (j = 1; j < props[i].length; j++) {
212
213
214
215
216 phost = NetProperties.get(props[i][j]+"Host");
217 if (phost != null && phost.length() != 0)
218 break;
219 }
220 if (phost == null || phost.length() == 0) {
221
222
223
224
225
226
227 if (hasSystemProxies) {
228 String sproto;
229 if (proto.equalsIgnoreCase("socket"))
230 sproto = "socks";
231 else
232 sproto = proto;
233 Proxy sproxy = getSystemProxy(sproto, urlhost);
234 if (sproxy != null) {
235 return sproxy;
236 }
237 }
238 return Proxy.NO_PROXY;
239 }
240
241
242 if (nprop != null) {
243 nphosts = NetProperties.get(nprop.property);
244 synchronized (nprop) {
245 if (nphosts == null) {
246 if (nprop.defaultVal != null) {
247 nphosts = nprop.defaultVal;
248 } else {
249 nprop.hostsSource = null;
250 nprop.hostsPool = null;
251 }
252 }
253 if (nphosts != null) {
254 if (!nphosts.equals(nprop.hostsSource)) {
255 RegexpPool pool = new RegexpPool();
256 StringTokenizer st = new StringTokenizer(nphosts, "|", false);
257 try {
258 while (st.hasMoreTokens()) {
259 pool.add(st.nextToken().toLowerCase(), Boolean.TRUE);
260 }
261 } catch (sun.misc.REException ex) {
262 }
263 nprop.hostsPool = pool;
264 nprop.hostsSource = nphosts;
265 }
266 }
267 if (nprop.hostsPool != null &&
268 nprop.hostsPool.match(urlhost) != null) {
269 return Proxy.NO_PROXY;
270 }
271 }
272 }
273
274
275 pport = NetProperties.getInteger(props[i][j]+"Port", 0).intValue();
276 if (pport == 0 && j < (props[i].length - 1)) {
277
278
279
280 for (int k = 1; k < (props[i].length - 1); k++) {
281 if ((k != j) && (pport == 0))
282 pport = NetProperties.getInteger(props[i][k]+"Port", 0).intValue();
283 }
284 }
285
286
287 if (pport == 0) {
288 if (j == (props[i].length - 1))
289 pport = defaultPort("socket");
290 else
291 pport = defaultPort(proto);
292 }
293
294
295
296 saddr = InetSocketAddress.createUnresolved(phost, pport);
297
298 if (j == (props[i].length - 1)) {
299 int version = NetProperties.getInteger(SOCKS_PROXY_VERSION, 5).intValue();
300 return SocksProxy.create(saddr, version);
301 } else {
302 return new Proxy(Proxy.Type.HTTP, saddr);
303 }
304 }
305 }
306 return Proxy.NO_PROXY;
307 }});
308
309 proxyl.add(p);
310
311
312
313
314
315 return proxyl;
316 }
317
318 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
319 if (uri == null || sa == null || ioe == null) {
320 throw new IllegalArgumentException("Arguments can't be null.");
321 }
322
323 }
324
325
326 private int defaultPort(String protocol) {
327 if ("http".equalsIgnoreCase(protocol)) {
328 return 80;
329 } else if ("https".equalsIgnoreCase(protocol)) {
330 return 443;
331 } else if ("ftp".equalsIgnoreCase(protocol)) {
332 return 80;
333 } else if ("socket".equalsIgnoreCase(protocol)) {
334 return 1080;
335 } else if ("gopher".equalsIgnoreCase(protocol)) {
336 return 80;
337 } else {
338 return -1;
339 }
340 }
341
342 private native static boolean init();
343 private native Proxy getSystemProxy(String protocol, String host);
344 }